package com.mq.consumer;

import com.alibaba.fastjson.JSON;
import com.common.entity.GlobalMsg;
import com.common.entity.Rsp;
import com.mq.feign.UserFeign;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@Component
public class GlobalConsumer {

    @Autowired
    UserFeign userFeign;

    //手动确认，出现异常
//    @RabbitListener(queues = "spring.Consumer1.mq")
//    public void consumer1 (Channel channel, String msg, Message message) throws IOException {
//        try {
//            System.out.println(msg);
//            channel.basicAck( message.getMessageProperties().getDeliveryTag(),false);
//        } catch (Exception e) {
//            System.out.println("消费者1接收到消息队列的消息出现异常");
//            channel.basicNack( message.getMessageProperties().getDeliveryTag(),false,true);
//        }
//    }

    //无异常
//    @RabbitListener(queues = "spring.global.mq")
//    public void consumer2 (Channel channel,String msg,Message message) throws IOException {
//        channel.basicAck( message.getMessageProperties().getDeliveryTag(),false);
//        System.out.println("消费者2接收到消息队列的消息：" + msg);
//        Map<String,Object> map = (Map<String, Object>) JSON.parse(msg);
//        GlobalMsg globalMsg = new GlobalMsg();
//        globalMsg.setMsgId(UUID.randomUUID().toString().replaceAll("-",""));
//        globalMsg.setUserId((String) map.get("userId"));
//        globalMsg.setMsgBody((String) map.get("msgBody"));
//        globalMsg.setMsgType((String) map.get("msgType"));
//        globalMsg.setDeleted(0);
//        globalMsg.setMsgStatus(0);
//        Rsp rsp = userFeign.addOneMsg(globalMsg);
//        System.out.println(rsp.toString());
//    }
}
